package com.kaisu.blog.infrastructure.user.convert;

import com.kaisu.blog.domain.user.Follow;
import com.kaisu.blog.domain.user.Role;
import com.kaisu.blog.domain.user.User;
import com.kaisu.blog.domain.user.vo.Password;
import com.kaisu.blog.infrastructure.user.FollowPO;
import com.kaisu.blog.infrastructure.user.RolePO;
import com.kaisu.blog.infrastructure.user.UserPO;
import com.kaisu.blog.infrastructure.user.convert.decorated.UserConvertDecorated;
import org.mapstruct.DecoratedWith;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingConstants;

/**
 * @author kaisu
 */

@Mapper(componentModel = MappingConstants.ComponentModel.SPRING, imports = {Password.class})
@DecoratedWith(UserConvertDecorated.class)
public interface UserConvert {
    @Mapping(target = "avatar", ignore = true)
    @Mapping(target = "password", expression = "java(new Password(po.getPassword()))")
    @Mapping(target = "authorities", ignore = true)
    User toDomain(UserPO po);

    @Mapping(target = "avatarId", source = "avatar.id")
    @Mapping(target = "updateTime", ignore = true)
    @Mapping(target = "createTime", ignore = true)
    UserPO toPO(User user);

    Role map(RolePO rolePO);

    @Mapping(target = "updateTime", ignore = true)
    @Mapping(target = "createTime", ignore = true)
    RolePO map(Role role);

    @Mapping(target = "follower.followers", ignore = true)
    @Mapping(target = "following.followers", ignore = true)
    @Mapping(target = "follower.followings", ignore = true)
    @Mapping(target = "following.followings", ignore = true)
    @Mapping(target = "follower.avatarId", source = "follower.avatar.id")
    @Mapping(target = "follower.updateTime", ignore = true)
    @Mapping(target = "follower.createTime", ignore = true)
    @Mapping(target = "following.avatarId", source = "following.avatar.id")
    @Mapping(target = "following.updateTime", ignore = true)
    @Mapping(target = "following.createTime", ignore = true)
    FollowPO map(Follow follow);

    @Mapping(target = "follower.followers", ignore = true)
    @Mapping(target = "following.followers", ignore = true)
    @Mapping(target = "follower.followings", ignore = true)
    @Mapping(target = "following.followings", ignore = true)
    @Mapping(target = "follower.avatar", ignore = true)
    @Mapping(target = "follower.password", ignore = true)
    @Mapping(target = "follower.authorities", ignore = true)
    @Mapping(target = "following.avatar", ignore = true)
    @Mapping(target = "following.password", ignore = true)
    @Mapping(target = "following.authorities", ignore = true)
    Follow map(FollowPO po);
}
